import config from "../config.js"

/**
 * 
 * 
 * @param {*} method type、post
 * @param {*} url 请求连接
 * @param {*} data 请求数据
 */
function request(method = 'get', url, data) {
	let platform = "";
	//#ifdef MP-WEIXIN
	platform = "wxmini";
	//#endif
	console.info(uni.getSystemInfoSync().platform)
	data = { ...data,
		// platform
	};
	let header = {}
	if (method.toUpperCase() === 'GET') {
		header['token'] = uni.getStorageSync('token');
	} else { //POST,
		header['content-type'] = 'application/x-www-form-urlencoded';
		header['token'] = uni.getStorageSync('token');
	}
	return new Promise((resolve, reject) => {
		url = url.includes("http") ? url : config.API + url;
		uni.request({
			url,
			method,
			header,
			data,
			success: res => {
				console.log("请求结果",res);
				if (res.statusCode != 200) {
					reject({
						type: 1,
						code: res.statusCode,
						msg: res.data.msg
					})
				}
				// if (res.data.code != 200) {
				// 	reject({
				// 		type: 2,
				// 		code: res.data.code,
				// 		msg: res.data.msg
				// 	})
				// }
				resolve(res.data)
			},
			fail: err => {
				reject({
					type: 0,
					msg: '请求失败'
				})
			} //这里会有错误信息,例如 err.errMsg:"request:fail"
		})
	})
}
['get', 'post'].forEach(
	v => (request[v] = (url, data) => request(v, url, data))
);

function error(err, _this) {
	uni.hideLoading()
	uni.stopPullDownRefresh()
	console.info("错误", err);
	switch (err.type) {
		case 0: //请求失败错误
			showErrorMsg(err.msg)
			break;
		case 1: //http_code错误
			if (err.code == 210) {
				uni.showToast({
					title: err.msg,
					duration: 2000,
					icon: 'none'
				});
			} else if (err.code == 401) {
				//未登录
				// showErrorMsg(err.msg)
				// uni.showModal({
				// 	title: '登录异常',
				// 	content: '请先登录,登录后可查看更多!',
				// 	showCancel: true,
				// 	confirmText: '登录',
				// 	success: (res) => {
				// 		// console.log(res);
				// 		if (res.confirm) {
				// 			uni.navigateTo({
				// 				url: "/pages/index/wxLogin"
				// 			})
				// 		} else if (res.cancel) {
				// 			console.log('用户点击取消');
				// 		}

				// 	}
				// });
			} else if (err.code == 203) {
				//前往填写资料
				uni.showModal({
					title: '提示',
					content: err.msg,
					showCancel: true,
					confirmText: '去填写',
					cancelColor: '#999999',
					success: (res) => {
						// console.log(res);
						if (res.confirm) {
							uni.navigateTo({
								url: "/pages/my/baseInfo"
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}

					}
				});
			} else {
				showErrorMsg(err.msg)
			}
			break;
		case 2:
			if (err.code == 800) { //跳转登录
				// _this.$refs.login.toLogin()
				// _this.logout()
				console.log('800:' + err.msg);
			} else if (err.code == 3) {
				uni.showModal({
					title: '提示',
					content: err.msg,
					showCancel: false,
					success() {
						uni.navigateBack({
							delta: 1
						})
					}
				});
			} else if (err.code == 4) {
				//不是VIP
				console.log(err);
			} else {
				showErrorMsg(err.msg)
			}
			break;
		default: //
			showErrorMsg('未知错误')
			break;
	}
}

function showErrorMsg(msg, code) {
	if (msg.length > 10) {
		uni.hideLoading();
		uni.showModal({
			title: '提示',
			content: msg,
			showCancel: false
		});
	} else {
		uni.showToast({
			title: msg,
			duration: 2000,
			icon: 'none'
		});
	}

}
export default {
	request,
	error
};
